﻿// ExecFileCommand.cs is part of AGatherBot.
//
// AGatherBot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// AGatherBot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public version 2 License for more details.
//
// You should have received a copy of the GNU General Public License
// along with AGatherBot. If not, see <http://www.gnu.org/licenses/>.

using AGatherBot;
using System;
using System.IO;

namespace AGatherBot.Commands
{
    public class ExecFileCommand : ISharpCommand
    {
        public string CommandName
        {
            get
            {
                return ("ExecFile");
            }
        }

        public string Execute(object sender, string input)
        {
            // TODO: Dumb-proof this.
            string file = input.Split(new char[] { ' ' }, 2)[1];

            // If the given file doesn't exist, display an error.
            if (!File.Exists(file))
            {
                Console.Error.WriteLine("*** Error: File wasn't found.");
            }
            // If the file exists, load the addons.
            else
            {
                Console.WriteLine("*** Executing file: {0}.", file);

                // Create the file stream to read from the addons file.
                using (var fstCommands = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // Create the stream reader to read from the file stream.
                    using (var srrCommands = new StreamReader(fstCommands))
                    {
                        // While we haven't reached the end of the stream, parse the stream.
                        while (!srrCommands.EndOfStream)
                        {
                            // Read current line and trim it.
                            var currLine = srrCommands.ReadLine().Trim();

                            // If the current line is a comment line or empty, pass over it.
                            if (currLine.StartsWith("#") || currLine.StartsWith("//") || string.IsNullOrWhiteSpace(currLine))
                            {
                                continue;
                            }
                            // Else, execute the line as an input.
                            else
                            {
                                Program.ExecuteInput(currLine);
                            }
                        }
                    }
                }
            }

            return (string.Format("Executed file: {0} successfuly.", file));
        }

        public string Help(object sender, string input)
        {
            return (string.Empty);
        }
    }
}